/*
 * Copyright (c) 2012 swing-on-fire Team
 * 
 * This file is part of Swing-On-Fire (http://code.google.com/p/swing-on-fire), licensed under the terms of the MIT
 * License (MIT).
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.google.code.swingonfire.component.page.old;

import java.awt.AlphaComposite;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import com.google.code.swingonfire.animation.AnimationUtil;
import com.google.code.swingonfire.image.ComponentBufferImageProvider;
import com.google.code.swingonfire.image.ImageProvider;
import com.google.code.swingonfire.utility.interpolation.Interpolation;

public class JPageCompositeAnimation extends AbstractJPageAnimation
{

    private final ImageProvider previousImageProvider;
    private final ImageProvider nextImageProvider;

    private final JPageComposite composite;
    private final Interpolation interpolation;

    private double previousValue;
    private double value;

    public JPageCompositeAnimation(double duration, JPageComposite composite, Interpolation interpolation,
        final Component pageComponent, Component nextComponent, Component previousComponent)
    {
        super(duration, pageComponent, nextComponent, previousComponent);

        previousImageProvider = new ComponentBufferImageProvider(previousComponent, true)
        {

            @Override
            protected void drawImage(Graphics2D g, int width, int height)
            {
                g.setColor(pageComponent.getBackground());
                g.fillRect(0, 0, width, height);
                
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
                g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

                super.drawImage(g, width, height);
            }

        };
        nextImageProvider = new ComponentBufferImageProvider(nextComponent, false);

        this.composite = composite;
        this.interpolation = interpolation;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void animationReset()
    {
        super.animationReset();

        value = previousValue = interpolation.calculate(0);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public double animationStep(double delay, double time, double fraction)
    {
        previousValue = value;
        value = interpolation.calculate(fraction);

        return AnimationUtil.animationResult(0, time, getDuration());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void renderAnimation(Graphics2D graphics, int width, int height)
    {
        Graphics2D g = previousImageProvider.getGraphics();

        composite.prepare(g, value, previousValue, width, height);

        g.setComposite(AlphaComposite.DstOver);
        g.drawImage(nextImageProvider.getImage(), 0, 0, null);

        graphics.drawImage(previousImageProvider.getImage(), 0, 0, null);
    }

}
